package com.hyb.algorithm.data.struct.stack;

import java.util.Stack;

/**
 * @Author: huyanbing
 * @Date: 2021/9/5 10:26 上午
 * <p>
 * https://leetcode-cn.com/problems/daily-temperatures/
 * 739. 每日温度
 */
public class DailyTemperatures {

    public static void main(String[] args) {
        int[] temperatures = new int[]{73, 74, 75, 71, 69, 72, 76, 73};
        DailyTemperatures exe = new DailyTemperatures();

        int[] res = exe.dailyTemperatures(temperatures);

        for(int i=0;i<res.length;i++){
            System.out.print(","+res[i]);
        }

    }


    public int[] dailyTemperatures(int[] temperatures) {

        Stack<Integer> stack = new Stack<Integer>();

        int len = temperatures.length;
        int[] res = new int[len];

        for (int i = len - 1; i >= 0; i--) {
            while (!stack.isEmpty() && (temperatures[i] >= temperatures[stack.lastElement()])) {
                stack.pop();
            }

            int ret = stack.isEmpty() ? 0 : stack.lastElement()-i;
            res[i] = ret;
            stack.push(i);
        }

        return res;
    }
}
